package cn.lijigou.shopping;

import java.util.concurrent.ExecutorService;

import cn.lijigou.shopping.domain.API;
import cn.lijigou.shopping.service.MSystem;
import cn.lijigou.shopping.util.GetSingleThreadPool;
import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

/**
 * @author 赵奇 E-mail:cagezhao@gmail.com
 * @version 创建时间：2012-4-6 下午04:53:15
 * 类说明
 */

public class FAQActivity extends MActivity {
	private WebView webView;
	int x,lastX,init;
	private ProgressBar progressBar = null;
	private MSystem mSystem = null;
	private ExecutorService executorService = GetSingleThreadPool.getInstance();
	
	private Handler waitHander = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == 0) {
				executorService.execute(new Thread() {
					public void run() {
						webView.loadUrl(API.FAQ);
					}
				});

			}
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		overridePendingTransition(R.anim.push_in,R.anim.push_out);
		getWindow().setFormat(PixelFormat.TRANSPARENT);
		setContentView(R.layout.faq);
		mSystem = new MSystem();
		webView = (WebView) findViewById(R.id.webview);
		progressBar = (ProgressBar) this.findViewById(R.id.progressBar);
		WebSettings setting = webView.getSettings();
		setting.setBuiltInZoomControls(false);
		setting.setSupportZoom(false);
		setting.setJavaScriptEnabled(true);
		setting.setSupportMultipleWindows(false);
		setting.setLightTouchEnabled(true);
		//优先使用缓存：  
		webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);    
//		不使用缓存：  
//		webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); 
		webView.setBackgroundColor(0); 
		webView.setHorizontalScrollBarEnabled(true);
		webView.setHorizontalScrollbarOverlay(true);
		webView.setVerticalScrollBarEnabled(true);
		webView.setVerticalScrollbarOverlay(true);
		
		webView.requestFocus();
		webView.setDownloadListener(new DownloadListener() {
            public void onDownloadStart(String url, String userAgent,
                            String contentDisposition, String mimetype,
                            long contentLength) {
                    Uri uri = Uri.parse(url);
                    Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                    startActivity(intent);
            }
		});
		webView.setWebViewClient(new WebViewClient() {
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				return super.shouldOverrideUrlLoading(view, url);
			}
		});
		webView.setWebChromeClient(new WebChromeClient(){
			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				view.getProgress();
				if(newProgress==100){
					progressBar.setVisibility(View.GONE);
				}else{
					progressBar.setVisibility(View.VISIBLE);
					progressBar.setProgress(newProgress);
				}
				super.onProgressChanged(view, newProgress);
			}
		});
		waitHander.sendEmptyMessage(0);
		
		
		
 
		
	}
	@Override
	protected void onDestroy() {
		webView.destroy();
		super.onDestroy();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(webView.canGoBack()){
			webView.goBack(); 
			return true;
         }else{
        	 return super.onKeyDown(keyCode, event);
         }
	}
}
